import csv
from django.db import transaction
from okuz.profile.models import *
from okuz.visit.models import *


@transaction.commit_manually        
def load_gender ():
	reader = csv.reader (open('db/plec.csv'), delimiter = ';');
	try:
		for r in reader:
			g = Gender (name = r[1]);
			g.save ();
	except: 
		transaction.rollback ();
		return 'Plec - nastapil blad podczas importowania danych';
	else:
		transaction.commit ();
		return 'Plec - dane zostaly poprawnie zaimportowane';

@transaction.commit_manually      
def load_maritalstatus ():
	reader = csv.reader (open('db/stancywilny.csv'), delimiter = ';');
	try:
		for r in reader:
			s = MaritalStatus (name = r[1]);
			s.save ();
	except: 
		transaction.rollback ();
		return 'Stan cywilny - nastapil blad podczas importowania danych';
	else:
		transaction.commit ();
		return 'Stan cywilny - dane zostaly poprawnie zaimportowane';

@transaction.commit_manually        
def load_profile ():
	reader = csv.reader (open('db/pacjenci.csv'), delimiter = ';');
	try:
		g1 = Gender.objects.get (id = 1);
		g2 = Gender.objects.get (id = 2);
		for r in reader:
			if r[11] == '0' or r[11] == None:
				g = g1;
			else:
				g = g2;
			s = MaritalStatus.objects.get (id = int(r[13])+1);
			if r[7] and r[7] != '0' and r[8] and r[8] != '0' and r[9] and r[9] != '0':
				d = ('%s-%s-%s' % (r[9], r[8], r[7]));
			else:
				d = None;
			p = Profile (first_name = r[1], last_name = r[2], street = r[3], postal_code = r[4], city = r[5],
						birthday = d, personal_id = r[10], gender = g, 
						marital_status = s, family_status = r[14], workplace = r[15], conditions = r[16],
						medical_history = r[17], family_illness = r[18]);
			p.save ();
	except: 
		transaction.rollback ();
		return 'Pacjenci - nastapil blad podczas importowania danych';
	else:
		transaction.commit ();
		return 'Pacjenci - dane zostaly poprawnie zaimportowane';

@transaction.commit_manually        
def load_comment ():
	reader = csv.reader (open('db/uwagi.csv'), delimiter = '	');
	c = None;
	try:
		for r in reader:
			c = Comment (id = r[0], name = r[1]);
			c.save ();
	except: 
		transaction.rollback ();
		return 'Uwagi - nastapil blad podczas importowania danych %s' % c.id;
	else:
		transaction.commit ();
		return 'Uwagi - dane zostaly poprawnie zaimportowane';

@transaction.commit_manually        
def load_visit ():
	reader = csv.reader (open('db/wizyty.csv'), delimiter = ';');
	try:
		for r in reader:
			u = ''; z = '';
			d = ('%s-%s-%s' % (r[3], r[2], r[1]));
			p = Profile.objects.get (id = int(r[5]));
			if int (r[6]) > 0:
					u = Comment.objects.get (id = int (r[6]));
			if int (r[14]) > 0:
				z = Comment.objects.get (id = int (r[14]));
			if u and z:
				v = Visit (date = d, profile = p, comment = u, recommendation = z);
			elif u:
				v = Visit (date = d, profile = p, comment = u);
			elif z:
				v = Visit (date = d, profile = p, recommendation = z);
			else:
				v = Visit (date = d, profile = p);
			v.save ();
	except: 
		transaction.rollback ();
		return 'Wizyty - nastapil blad podczas importowania danych';
	else:
		transaction.commit ();
		return 'Wizyty - dane zostaly poprawnie zaimportowane';

